home *** CD-ROM | disk | FTP | other *** search
GNU Info File | 1993-11-06 | 20.7 KB | 530 lines |
- This is Info file socket++.info, produced by Makeinfo-1.49 from the
- input file socket++.texi.
-
- This info file describes the C++ family of socket classes.
-
- Copyright (C) 1992,1993 Gnanasekaran Swaminathan <gs4t@virginia.edu>
-
- Permission is granted to make and distribute verbatim copies of this
- document provided the copyright notice and this permission notice are
- preserved on all copies.
-
- File: socket++.info, Node: iosockunix, Prev: iosockinet, Up: sockstream Classes
-
- iosockunix Classes
- ==================
-
- We discuss only `isockunix' here. `osockunix' and `iosockunix' are
- similar.
-
- isockunix class
- ---------------
-
- `isockunix' is used to handle interprocess communication in UNIX
- domain. It is derived from `isockstream' class and it uses a
- `sockunixbuf' as its stream buffer. *Note iosockstream::, for more
- details on `isockstream'. *Note sockunixbuf Class::, for information on
- `sockunixbuf'.
-
- In what follows,
- - `ty' is a `sockbuf::type' and must be one of
- `sockbuf::sock_stream', `sockbuf::sock_dgram',
- `sockbuf::sock_raw', `sockbuf::sock_rdm', and
- `sockbuf::sock_seqpacket'
-
- - `proto' denotes the protocol number and is of type int
-
- - `sb' is a `sockbuf' object and must be in UNIX domain
-
- - `sinp' is a pointer to an object of `sockunixbuf'
-
- `isockunix is (ty, proto)'
- constructs an `isockunix' object `is' whose `sockunixbuf' buffer
- is of the type `ty' and has the protocol number `proto'. The
- default protocol number is 0.
-
- `isockunix is (sb)'
- constructs a `isockunix' object `is' whose `sockunixbuf' is `sb'.
- `sb' must be in UNIX domain.
-
- `isockunix is (sinp)'
- constructs a `isockunix' object `is' whose `sockunixbuf' is `sinp'.
-
- `sinp = is.rdbuf ()'
- returns a pointer to the `sockunixbuf' of `isockunix' object `is'.
-
- `isockunix::operator ->'
- returns `sockunixbuf' of `sockunix' so that the `sockunix' object
- acts as a smart pointer to `sockunixbuf'.
-
- is->localhost (); // same as is.rdbuf ()->localhost ();
-
- iosockunix examples
- -------------------
-
- `tsunread' listens for connections. When `tsunwrite' requests
- connection, `tsunread' accepts it and waits for input. `tsunwrite'
- sends the string "Hello!!!" to `tsunread'. `tsunread' reads the string
- sent by `tsunwrite' and prints on its stdout.
-
- // tsunread.cc
- #include <sockunix.h>
- extern "C" int unlink (const char*);
- main ()
- {
- sockunixbuf sunb (sockbuf::sock_stream);
- unlink ("/tmp/socket+-");
- sockunixaddr suna ("/tmp/socket+-");
- sunb.bind (suna);
- sunb.listen (2);
- isockunix is = sunb.accept ();
- char buf[32];
- is >> buf; cout << buf << endl;
- }
-
- // tsunwrite.cc
- #include <sockunix.h>
- main ()
- {
- osockunix os (sockbuf::sock_stream);
- sockunixaddr suna ("/tmp/socket++");
- os->connect (suna);
- os << "Hello!!!\n";
- }
-
- File: socket++.info, Node: pipestream Classes, Next: Error Handling, Prev: sockstream Classes, Up: Top
-
- pipestream Classes
- ******************
-
- `pipestream' stream classes provide the services of the UNIX system
- calls `pipe' and `socketpair' and the C library function `popen'.
- `ipipestream', `opipestream', and `iopipestream' are obtained by simply
- deriving from `isockstream', `osockstream' and `iosockstream'
- respectively. *Note sockstream Classes:: for details.
-
- In what follows,
- - `ip' is an `ipipestream' object
-
- - `op' is an `opipestream' object
-
- - `iop' is an `iopipestream' object
-
- - `cmd' is a char* denoting an executable like "wc"
-
- - `ty' is of type `sockbuf::type' indicating the type of the
- connection
-
- - `proto' is an `int' denoting a protocol number
-
- `ipipestream ip(cmd)'
- construct an `ipipestream' object `ip' such that the output of the
- command `cmd' is available as input through `ip'.
-
- `opipestream op(cmd)'
- construct an `opipestream' object `op' such that the input for the
- command `cmd' can be send through `op'.
-
- `iopipestream iop(cmd)'
- construct an `iopipestream' object `iop' such that the input and
- the output to the command `cmd' can be sent and received through
- `iop'.
-
- `iopipestream iop(ty, proto)'
- construct a `iopipestream' object `iop' whose socket is a
- socketpair of type `ty' with protocol number `proto'. `ty'
- defaults to `sockbuf::sock_stream' and `proto' defaults to 0.
- Object `iop' can be used either as a `pipe' or as a `socketpair'.
-
- `iop.pid ()'
- return the process id of the child if the current process is the
- parent or return 0. If the process has not forked yet, return -1.
-
- `iopipestream::fork ()'
- `fork()' is a static function of class `iopipestream'. `fork()'
- forks the current process and appropriately sets the `cpid' field
- of the `iopipestream' objects that have not forked yet.
-
- * Menu:
-
- * pipe Example:: How to use pipestream as pipe?
- * socketpair Example:: How to use pipestream as socketpair?
- * popen Example:: How to use pipestream as popen?
-
- File: socket++.info, Node: pipe Example, Next: socketpair Example, Up: pipestream Classes
-
- pipestream as pipe
- ==================
-
- `pipe' is used to communicate between parent and child processes in
- the UNIX domain.
-
- The following example illustrates how to use `iopipestream' class as
- a `pipe'. The parent sends the string "I am the parent" to the child
- and receives the string "I am the child" from child. The child, in
- turn, receives the string "I am the parent" from parent and sends the
- string "I am the child" to the parent. Note the same `iopipestream'
- object is used for input and output in each process.
-
- #include <pipestream.h>
-
- main()
- {
- iopipestream p;
- if ( p.fork() ) {
- char buf[128];
- p << "I am the parent\n";
- cout << "parent: ";
- while(p >> buf)
- cout << buf << ' ';
- cout << endl;
- }else {
- char buf[128];
- p.getline(buf, 127);
- cout << "child: " << buf << endl;
- p << "I am the child\n";
- }
- }
-
- File: socket++.info, Node: socketpair Example, Next: popen Example, Prev: pipe Example, Up: pipestream Classes
-
- pipestream as socketpair
- ========================
-
- Like pipes, socketpairs also allow communication between parent and
- child processes. But socketpairs are more flexible than pipes in the
- sense that they let the users choose the socket type and protocol.
-
- The following example illustrates the use of `iopipestream' class as
- a `socketpair' whose type is `sockbuf::sock_dgram'. The parent sends
- the string "I am the parent" to the child and receives the string "I am
- the child" from the child. The child, in turn, receives and sends the
- strings "I am the parent" and "I am the child" respectively from and to
- the parent. Note in the following example that the same `iopipestream'
- object is used for both the input and the output in each process.
-
- #include <pipestream.h>
-
- main()
- {
- iopipestream p(sockbuf::sock_dgram);
- if ( iopipestream::fork() ) {
- char buf[128];
- p << "I am the parent\n";
- p.getline(buf, 127);
- cout << "parent: " << buf << endl;
- }else {
- char buf[128];
- p.getline(buf, 127);
- cout << "child: " << buf << endl;
- p << "I am the child\n";
- }
- }
-
- File: socket++.info, Node: popen Example, Prev: socketpair Example, Up: pipestream Classes
-
- pipestream as popen
- ===================
-
- `popen' is used to call an executable and send inputs and outputs to
- that executable. For example, the following function executes
- "/bin/date", gets its output, and prints it to stdout.
-
- #include <pipestream.h>
-
- main ()
- {
- char buf[128];
- ipipestream p("/bin/date");
-
- p.getline (buf, 127);
- cout << buf << endl;
- }
-
- Here is an example that prints "Hello World!!" on stdout. It uses
- `opipestream' object.
-
- #include <pipestream.h>
-
- main ()
- {
- opipestream p("/bin/cat");
- p << "Hello World!!\n";
- }
-
- The following example illustrates the use of `iopipestream' for both
- input and output.
-
- #include <pipestream.h>
-
- main()
- {
- char buf[128];
- iopipestream p("lpc");
- p << "help\nquit\n";
- while ( p.getline(buf, 127) ) cout << buf << endl;
- }
-
- File: socket++.info, Node: Error Handling, Next: Pitfalls, Prev: pipestream Classes, Up: Top
-
- Error Handling
- **************
-
- Each class in the Socket++ library uses `error(const char*)' member
- function to report any errors that may occur during a system call. It
- first calls `perror()' to report the error message for the `errno' set
- by the system call. It then calls `(*lib_error_handler) (const char*
- nm, const char* errmsg)' where nm is the name of the class.
-
- If the user wants to handle the error more gracefully he can define
- his own error handler instead of the default `lib_error_handler'.
-
- In what follows,
- - `eh' is of type void (*)(const char*, const char*)
-
- `set_lib_error_handler(eh)'
- returns the old lib error handler after setting `lib_error_handler'
- to eh. This function is part of `libg++' library and is not part
- of Socket++ library.
-
- File: socket++.info, Node: Pitfalls, Next: Index, Prev: Error Handling, Up: Top
-
- Pitfalls
- ********
-
- Deadlocks in datagram sockets are the most common mistakes that
- novices make. To alleviate the problem, `sockbuf' class provides timeout
- facilities that can be used effectively to avoid deadlocks.
-
- Consider the following simple tsmtp example which sends the HELP
- command to a smtp server and gets back the help message. Suppose it
- does not know the size of the help message nor the format of the
- message. In such cases, the timeout facilities of `sockbuf' class
- provides the required tools.
-
- The example terminates the help message reception if the there is no
- input activity from the smtp server for 10 seconds.
-
- tsmtp.cc
- --------
-
- #include <sockinet.h>
-
- main()
- {
- sockinetaddr sina("kelvin.seas.virginia.edu", "smtp", "tcp");
- iosockinet sio(sockbuf::sock_stream);
-
- sio->connect(sina);
-
- char buf[512];
- sio.getline(buf, 511); cout << buf << endl;
- sio << "HELO kelvin\n";
- sio.getline(buf, 511); cout << buf << endl;
-
- sio << "HELP\n";
-
- // set the receive timeout to 10 seconds
- int tmo = sio->recvtimeout(10);
-
- while ( sio.getline(buf, 511) ) cout << buf << endl;
- // if the above while loop terminated due to timeout
- // clear the state of sio.
- if ( !sio->is_eof() )
- sio.clear();
- sio->recvtimeout(tmo); // reset the receive timeout time
-
- sio << "QUIT\n";
- sio.getline(buf, 511); cout << buf << endl;
- }
-
- File: socket++.info, Node: Index, Prev: Pitfalls, Up: Top
-
- Index
- *****
-
- * Menu:
-
- * accepting connections: Connection Establishment.
- * acknowledgments: Acknowledgments.
- * base address class: sockAddr Class.
- * binding addresses: Connection Establishment.
- * class isockinet: iosockinet.
- * class isockunix: iosockunix.
- * class sockbuf: sockbuf Class.
- * common mistakes: Pitfalls.
- * connect: Connection Establishment.
- * connection establishment: Connection Establishment.
- * Copyright: Copying.
- * copyright notice: Copying.
- * datagram inet: Datagram INET.
- * datagram unix: Datagram UNIX.
- * error handling: Error Handling.
- * flushing output: Reading and Writing.
- * getpeername (see sockinetbuf::peeraddr): Methods sockinetbuf.
- * getsockname (see sockinetbuf::localaddr): Methods sockinetbuf.
- * getsockopt: Socket Options.
- * inet address class: sockinetaddr Class.
- * inet domain: sockinetbuf Class.
- * iopipestream::fork: pipestream Classes.
- * iopipestream::iopipestream: pipestream Classes.
- * iopipestream::pid: pipestream Classes.
- * iosockinet examples: iosockinet.
- * iosockstream class: iosockstream.
- * iosockstream classes: sockstream Classes.
- * iosockstream example: Stream INET.
- * iosockstream::iosockstream: iosockstream.
- * iosockstream::operator->: iosockstream.
- * iosockstream::rdbuf: iosockstream.
- * iosockunix class: iosockunix.
- * iosockunix examples: iosockunix.
- * ipipestream::ipipestream: pipestream Classes.
- * isockinet class: iosockinet.
- * isockinet::isockinet: iosockinet.
- * isockinet::operator->: iosockinet.
- * isockinet::rdbuf: iosockinet.
- * isockstream class: iosockstream.
- * isockstream example: Datagram UNIX.
- * isockstream example: Datagram INET.
- * isockstream::isockstream: iosockstream.
- * isockstream::operator->: iosockstream.
- * isockstream::rdbuf: iosockstream.
- * isockunix class: iosockunix.
- * isockunix::isockunix: iosockunix.
- * isockunix::operator->: iosockunix.
- * isockunix::rdbuf: iosockunix.
- * listening: Connection Establishment.
- * names: Connection Establishment.
- * opipestream::opipestream: pipestream Classes.
- * option getting: Socket Options.
- * option setting: Socket Options.
- * osockstream class: iosockstream.
- * osockstream example: Datagram INET.
- * osockstream example: Datagram UNIX.
- * osockstream::operator->: iosockstream.
- * osockstream::osockstream: iosockstream.
- * osockstream::rdbuf: iosockstream.
- * overview of socket++: Overview of Socket++.
- * pipe: pipestream Classes.
- * pipe example: pipe Example.
- * pipestream classes: pipestream Classes.
- * pipestream examples: pipestream Classes.
- * pitfalls: Pitfalls.
- * popen: pipestream Classes.
- * popen example: popen Example.
- * read timeouts: Timeouts.
- * setsockopt: Socket Options.
- * set_lib_error_handler: Error Handling.
- * sockAddr class: sockAddr Class.
- * sockAddr::family: sockAddr Class.
- * sockAddr::operator void*: sockAddr Class.
- * sockAddr::size: sockAddr Class.
- * sockbuf class: sockbuf Class.
- * sockbuf constructors: Constructors.
- * sockbuf destructor: Destructor.
- * sockbuf reading: Reading and Writing.
- * sockbuf writing: Reading and Writing.
- * sockbuf::accept: Connection Establishment.
- * sockbuf::bind: Connection Establishment.
- * sockbuf::broadcast: Socket Options.
- * sockbuf::clearerror: Socket Options.
- * sockbuf::close: Destructor.
- * sockbuf::connect: Connection Establishment.
- * sockbuf::debug: Socket Options.
- * sockbuf::doallocate: Reading and Writing.
- * sockbuf::dontroute: Socket Options.
- * sockbuf::flush_output: Reading and Writing.
- * sockbuf::getopt: Socket Options.
- * sockbuf::gettype: Socket Options.
- * sockbuf::is_eof: Reading and Writing.
- * sockbuf::is_exceptionpending: Reading and Writing.
- * sockbuf::is_open: Constructors.
- * sockbuf::is_open: Reading and Writing.
- * sockbuf::is_readready: Reading and Writing.
- * sockbuf::is_writeready: Reading and Writing.
- * sockbuf::keepalive: Socket Options.
- * sockbuf::linger: Socket Options.
- * sockbuf::listen: Connection Establishment.
- * sockbuf::msgflag: Reading and Writing.
- * sockbuf::msgflag: Reading and Writing.
- * sockbuf::oobinline: Socket Options.
- * sockbuf::open: Constructors.
- * sockbuf::operator=: Constructors.
- * sockbuf::overflow: Reading and Writing.
- * sockbuf::rcvbuf: Socket Options.
- * sockbuf::read: Reading and Writing.
- * sockbuf::recv: Reading and Writing.
- * sockbuf::recvfrom: Reading and Writing.
- * sockbuf::recvmsg: Reading and Writing.
- * sockbuf::recvtimeout: Reading and Writing.
- * sockbuf::recvtimeout: Timeouts.
- * sockbuf::reuseaddr: Socket Options.
- * sockbuf::send: Reading and Writing.
- * sockbuf::sendmsg: Reading and Writing.
- * sockbuf::sendtimeout: Reading and Writing.
- * sockbuf::sendtimeout: Timeouts.
- * sockbuf::sendto: Reading and Writing.
- * sockbuf::setopt: Socket Options.
- * sockbuf::shutdown: Destructor.
- * sockbuf::shuthow: Destructor.
- * sockbuf::sndbuf: Socket Options.
- * sockbuf::sockbuf: Constructors.
- * sockbuf::sync: Reading and Writing.
- * sockbuf::sys_read: Reading and Writing.
- * sockbuf::sys_write: Reading and Writing.
- * sockbuf::type: Constructors.
- * sockbuf::underflow: Reading and Writing.
- * sockbuf::write: Reading and Writing.
- * sockbuf::xsputn: Reading and Writing.
- * sockbuf::~sockbuf: Destructor.
- * socket options: Socket Options.
- * socketpair: pipestream Classes.
- * socketpair example: socketpair Example.
- * sockinetaddr class: sockinetaddr Class.
- * sockinetaddr::family: sockinetaddr Class.
- * sockinetaddr::getport: sockinetaddr Class.
- * sockinetaddr::getthostname: sockinetaddr Class.
- * sockinetaddr::operator void*: sockinetaddr Class.
- * sockinetaddr::size: sockinetaddr Class.
- * sockinetaddr::sockinetaddr: sockinetaddr Class.
- * sockinetbuf class: sockinetbuf Class.
- * sockinetbuf dgram example: Datagram INET.
- * sockinetbuf stream example: Stream INET.
- * sockinetbuf::localaddr: Methods sockinetbuf.
- * sockinetbuf::localhost: Methods sockinetbuf.
- * sockinetbuf::localport: Methods sockinetbuf.
- * sockinetbuf::open: Methods sockinetbuf.
- * sockinetbuf::operator =: Methods sockinetbuf.
- * sockinetbuf::peeraddr: Methods sockinetbuf.
- * sockinetbuf::peerhost: Methods sockinetbuf.
- * sockinetbuf::peerport: Methods sockinetbuf.
- * sockinetbuf::sockinetbuf: Methods sockinetbuf.
- * sockstream classes: sockstream Classes.
- * sockunixaddr class: sockunixaddr Class.
- * sockunixaddr::family: sockunixaddr Class.
- * sockunixaddr::operator void*: sockunixaddr Class.
- * sockunixaddr::size: sockunixaddr Class.
- * sockunixaddr::sockunixaddr: sockunixaddr Class.
- * sockunixbuf class: sockunixbuf Class.
- * sockunixbuf datagram example: Datagram UNIX.
- * sockunixbuf::open: Methods sockunixbuf.
- * sockunixbuf::operator =: Methods sockunixbuf.
- * sockunixbuf::sockunixbuf: Methods sockunixbuf.
- * stream inet: Stream INET.
- * stream unix: Stream UNIX.
- * timeout example: Pitfalls.
- * timeouts: Timeouts.
- * unix address class: sockunixaddr Class.
- * unix domain: sockunixbuf Class.
- * write timeouts: Timeouts.
-
-
-